package com.example.hrsystem.ui.user;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.hrsystem.data.model.User;
import com.example.hrsystem.data.repository.UserRepository;

import java.util.List;

/**
 * 用户ViewModel
 * 连接Repository和UI层，处理用户相关的UI数据逻辑
 */
public class UserViewModel extends AndroidViewModel {

    private UserRepository repository;
    private LiveData<List<User>> allUsers;
    private MutableLiveData<Boolean> isLoading = new MutableLiveData<>(false);
    private MutableLiveData<String> errorMessage = new MutableLiveData<>();
    private MutableLiveData<User> currentUser = new MutableLiveData<>();

    /**
     * 构造方法
     */
    public UserViewModel(@NonNull Application application) {
        super(application);
        repository = new UserRepository(application);
        allUsers = repository.getAllUsers();
    }

    /**
     * 获取所有用户
     */
    public LiveData<List<User>> getAllUsers() {
        return allUsers;
    }

    /**
     * 根据用户ID获取用户
     */
    public LiveData<User> getUserById(int userId) {
        return repository.getUserById(userId);
    }

    /**
     * 根据员工工号获取用户
     */
    public LiveData<User> getUserByEmployeeId(String employeeId) {
        return repository.getUserByEmployeeId(employeeId);
    }

    /**
     * 根据部门获取用户
     */
    public LiveData<List<User>> getUsersByDepartment(String department) {
        return repository.getUsersByDepartment(department);
    }

    /**
     * 获取在职员工
     */
    public LiveData<List<User>> getActiveUsers() {
        return repository.getActiveUsers();
    }

    /**
     * 根据用户类型获取用户
     */
    public LiveData<List<User>> getUsersByUserType(String userType) {
        return repository.getUsersByUserType(userType);
    }

    /**
     * 获取当前登录用户
     */
    public LiveData<User> getCurrentUser() {
        return currentUser;
    }

    /**
     * 设置当前登录用户
     */
    public void setCurrentUser(User user) {
        currentUser.setValue(user);
    }

    /**
     * 获取加载状态
     */
    public LiveData<Boolean> getIsLoading() {
        return isLoading;
    }

    /**
     * 获取错误信息
     */
    public LiveData<String> getErrorMessage() {
        return errorMessage;
    }

    /**
     * 添加用户
     */
    public void addUser(User user) {
        isLoading.setValue(true);
        errorMessage.setValue(null);

        try {
            // 检查用户是否已存在
            if (repository.isUserExistsSync(user.getUserId())) {
                errorMessage.setValue("用户已存在");
            } else {
                repository.insert(user);
            }
        } catch (Exception e) {
            errorMessage.setValue("添加用户失败：" + e.getMessage());
            e.printStackTrace();
        } finally {
            isLoading.setValue(false);
        }
    }

    /**
     * 更新用户信息
     */
    public void updateUser(User user) {
        isLoading.setValue(true);
        errorMessage.setValue(null);

        try {
            // 检查用户是否存在
            if (!repository.isUserExistsSync(user.getUserId())) {
                errorMessage.setValue("用户不存在");
            } else {
                repository.update(user);
            }
        } catch (Exception e) {
            errorMessage.setValue("更新用户失败：" + e.getMessage());
            e.printStackTrace();
        } finally {
            isLoading.setValue(false);
        }
    }

    /**
     * 加载当前用户信息
     */
    public void loadCurrentUser(int userId) {
        isLoading.setValue(true);
        errorMessage.setValue(null);

        try {
            User user = repository.getUserByIdSync(userId);
            if (user != null) {
                currentUser.setValue(user);
            } else {
                errorMessage.setValue("未找到用户信息");
            }
        } catch (Exception e) {
            errorMessage.setValue("加载用户信息失败：" + e.getMessage());
            e.printStackTrace();
        } finally {
            isLoading.setValue(false);
        }
    }
}